package xj.graph2d.template.uml;

import xj.graph2d.DrawShape;
import xj.graph2d.DrawShapeConstants;
import xj.graph2d.GraphEdgeBase;
import xj.graph2d.template.UMLClassDiagramTemplate;
import xj.graph2d.template.UMLTemplate;
import xj.graph2d.template.UMLUseCaseDiagramTemplate;
import xj.model.uml.UMLAssociation;

public class UMLDependencyModelListener extends UMLAbstractModelListener {

  public UMLDependencyModelListener() {
  }

  public UMLDependencyModelListener(UMLTemplate template) {
    this(null, null, template);
  }

  public UMLDependencyModelListener(Object model, DrawShape shape,
				    UMLTemplate template) {
    super(model, shape, template);
  }

  @Override
  public void updateModel() {
    if (shape instanceof GraphEdgeBase && model instanceof UMLAssociation) {
      GraphEdgeBase edge = (GraphEdgeBase) shape;
      UMLAssociation dmodel = (UMLAssociation) model;
      String label = edge.getLabel(DrawShapeConstants.ROLE_EDGE_LABEL);
      if (label != null) {
	int l = label.length();
	if (l >= 2 && 
	    label.charAt(0) == '\u00AB' && label.charAt(l - 1) == '\u00BB') {
	  label = label.substring(1, l - 1);
	}
      }
      dmodel.setSubtype(label);
    }
  }

  @Override
  public void updateShape() {
    if (shape instanceof GraphEdgeBase && model instanceof UMLAssociation) {
      GraphEdgeBase edge = (GraphEdgeBase) shape;
      UMLAssociation dmodel = (UMLAssociation) model;
      UMLAssociation.AssociationType type = dmodel.getType();
      String dtype = dmodel.getSubtype();
      String fontkey;
      String colorkey;
      if (type == UMLAssociation.AssociationType.UseCaseDependency) {
	fontkey = UMLClassDiagramTemplate.ASSOCIATION_LABEL_FONT;
	colorkey = UMLUseCaseDiagramTemplate.USE_CASE_DEPENDENCY_COLOR;
      } else {
	fontkey = UMLClassDiagramTemplate.ASSOCIATION_LABEL_FONT;
	colorkey = UMLClassDiagramTemplate.ASSOCIATION_LABEL_COLOR;
      }
      setLabel(edge, DrawShapeConstants.ROLE_EDGE_LABEL, dtype, "type",
	       "\u00AB", "\u00BB", false, fontkey, colorkey);
    }
  }

}
